from django.db import models
from user.models import UserModel
# Create your models here.
#优惠券
class CouponModel(models.Model):
    COUPON_TYPE_CHOICES = (
        ('1', '减免'),
        ('2', '折扣')
    )
    USE_RANGE_CHOICES = (
        ('1', '通用'),
        ('2', '指定商品')
    )

    coupon_name = models.CharField(max_length=50, verbose_name="优惠券名称", null=False, blank=False)
    coupon_type = models.IntegerField(choices=COUPON_TYPE_CHOICES, verbose_name="优惠券类型", null=False,
                                   blank=False)
    use_range = models.IntegerField(choices=USE_RANGE_CHOICES, verbose_name="使用范围", null=False, blank=False)
    amount = models.IntegerField(default=0, verbose_name="满多少可用")
    coupon = models.IntegerField(null=False, blank=False, verbose_name="优惠券面值/折扣")
    is_valid = models.BooleanField(default=True, verbose_name="是否有效")
    start_date = models.DateTimeField(auto_now_add=True, verbose_name="开始时间")
    expire_date = models.DateTimeField(auto_now_add=True, verbose_name="到期时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")


    class Meta:
        verbose_name = "优惠券"
        verbose_name_plural = "优惠券列表"


    def __str__(self):
        return self.coupon_name

# 优惠券使用记录
class UserCouponModel(models.Model):
    user = models.ManyToManyField(UserModel, related_name='user_coupons', verbose_name="用户")
    coupon = models.ManyToManyField(CouponModel, related_name='coupons_used', verbose_name="优惠券")
    status = models.IntegerField(choices=(
        (1, "未使用"),
        (2, "已使用"),
        (3, "已取消"),
        (4, "待支付")
    ), verbose_name="使用状态", default=0)
    receiving_time = models.DateTimeField(auto_now_add=True, verbose_name="领取时间")

    class Meta:
        verbose_name = "用户优惠券"
        verbose_name_plural = "用户优惠券列表"

    def __str__(self):
        return f"user: {self.user.all()}, coupon: {self.coupon.all()}"
# 积分
class PointsModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="关联的用户")
    transaction_type = models.IntegerField(choices=(
        (1, "增加"),
        (2, "消费")
    ), verbose_name="数据变动的类型")
    description = models.CharField(max_length=200, verbose_name="积分变化的详细描述")
    balance = models.IntegerField(verbose_name="积分")
    def __str__(self):
        return f"{self.user_id} - {self.transaction_type}: {self.balance}"
    class Meta:
        verbose_name = "积分表"
        verbose_name_plural = "积分表列表"

